package mn.idax.exchange.otc.util;

import mn.idax.exchange.common.SpringUtils;
import mn.idax.exchange.otc.domain.MessageNotifyDTO;
import mn.idax.exchange.otc.entity.User;
import mn.idax.exchange.otc.enums.MessageNotifyEnum;
import mn.idax.exchange.otc.enums.OrderState;
import mn.idax.exchange.otc.service.UserService;
import mn.idax.exchange.otc.service.impl.MessageNotifyService;

import java.text.MessageFormat;

/**
 * @Auther: dison
 * @Date: 2018/8/31 14:30
 * @Description:
 */
public class MessageNotifyUtils {

    /**
     *
     * 功能描述:
     *      发送消息通知
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/31 14:31
     */
    public static boolean sendNotify(Integer userId, MessageNotifyDTO messageNotify){
        MessageNotifyService messageNotifyService = SpringUtils.getBean(MessageNotifyService.class);
        return messageNotifyService.broadcastToUser(userId,messageNotify);
    }


    public static boolean sendNotify(Integer userId,Object skipKey, String body,MessageNotifyEnum messageNotifyEnum,MessageNotifyEnum.SkipType SkipType ,Object... dynamicParam){
        MessageNotifyDTO messageNotify = new MessageNotifyDTO();
        messageNotify.setMessageType(messageNotifyEnum);
        messageNotify.setSkipType(SkipType);
        messageNotify.setSkipKey(skipKey.toString());
        messageNotify.setBody(body);
        messageNotify.setDynamicParam(dynamicParam);
        return sendNotify(userId,messageNotify);
    }

    /**
     *
     * 功能描述:
     *      发送通知
     * @param:  userId 通知的用户
     * @param:  skipKey 跳转的主键
     * @param:  bodyTemplate 通知内容模版
     * @param:  obj 通知占位符内容
     * @return:
     * @auther: dison
     * @date: 2018/9/3 9:35
     */
    public static boolean sendNotify(Integer userId, Object skipKey, MessageNotifyEnum.BodyTemplate bodyTemplate, String... obj) {
        String body = bodyTemplate.getBodyCode();
        return sendNotify(userId,
                skipKey,
                body,
                bodyTemplate.getMessageNotifyEnum(),
                bodyTemplate.getSkipType(),
                obj);
    }

    public static String getNotifyNickName(Integer userId){
        UserService userService = SpringUtils.getBean(UserService.class);
        User user = userService.getUserById(userId);
        return NickNameUtils.concatNickName(user.getNickname() , user.getEmail());
    }

    public static String getChatNotifyContent(Integer orderState ,String... var){
        String templateContent = OrderState.PushMessageTemplate.getTemplate(orderState);
        return MessageFormat.format(templateContent,var);
    }
}
